﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.ManagementPlatform.SignatureManage
{
    public partial class SelectSignature : BasePage
    {
        private Sys_SignatureBLL ssb = new Sys_SignatureBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblUserName.Text = lognUser.USER_NAME.ToString();
                BindSign();
            }
        }

        protected void BindSign()
        {
            DataTable dtSignature = ssb.GetSignture(decimal.Parse(lognUser.USER_ID.ToString()), decimal.Parse(radlType.SelectedValue.ToString().Trim()));

            dropSignature.DataSource = dtSignature;
            dropSignature.DataTextField = "SIGNATURENAME";
            dropSignature.DataValueField = "SIGNATUREID";
            dropSignature.DataBind();
            dropSignature.Items.Insert(0, new ListItem("请选择签章", "-1"));
        }

        protected void radlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            imgPreview.ImageUrl = "~/App_Themes/Images/white.bmp";
            hfldSignatureID.Value = "0";
            BindSign();
        }

        protected void dropSignature_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropSignature.SelectedValue.ToString() == "-1")
            {
                imgPreview.ImageUrl = "~/App_Themes/Images/white.bmp";
            }
            else
            {
                DataTable dt = ssb.GetSignatureDataById(decimal.Parse(dropSignature.SelectedValue.ToString()));
                string url = dt.Rows[0]["SIGNATUREPATH"].ToString();

                imgPreview.ImageUrl = url.Replace("..", "~"); ;// "~/UpLoad/13.bmp";
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (dropSignature.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(this, "请选择签章");
                return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show(this, "请输入调用密码");
                return;
            }

            string password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text.Trim(), "MD5");
            bool isTrue = ssb.IsSignature(decimal.Parse(lognUser.USER_ID.ToString()), decimal.Parse(radlType.SelectedValue.Trim()), password);
            if (isTrue)
            {
                hfldSignatureID.Value = dropSignature.SelectedValue.ToString().Trim() + "_" + imgPreview.ImageUrl;
                MessageBox.ResponseScript(this, "window.returnValue= document.getElementById('hfldSignatureID').value; window.close();");
            }
            else
            {
                MessageBox.Show(this, "签章密码不对请重新输入");
                return;
            }
        }
    }
}